package com.mz.ieann.aide.city.view

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.mz.ieann.aide.R
import com.mz.ieann.aide.base.vm.BaseVMActivity
import com.mz.ieann.aide.city.adapter.HouseItemDecoration
import com.mz.ieann.aide.city.adapter.HouseListAdapter
import com.mz.ieann.aide.city.bean.*
import com.mz.ieann.aide.city.model.HouseViewModel
import com.mz.ieann.aide.databinding.ActivityHouseListBinding
import com.mz.ieann.dokit.sdk.SDKConstant
import com.mz.ieann.dokit.sdk.base.BaseDomain
import com.mz.ieann.dokit.utils.LogUtils

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz90
 * @date on 2021 2021/11/14 13:39
 */
class HouseListActivity : BaseVMActivity<HouseViewModel, ActivityHouseListBinding>(R.layout.activity_house_list),
    SwipeRefreshLayout.OnRefreshListener {

    /**
     * 适配器
     */
    private var adapter : HouseListAdapter? = null

    /**
     * 查询结果
     */
    private var houseResult : HouseResult? = null

    /**
     * 搜索关键字
     */
    private var keyWord = "杭州"

    /**
     * 翻页页数
     */
    private var start = 0

    override fun initActivity() {
        keyWord = intent?.extras?.getString("keyWord", "杭州").toString()
        Log.v(SDKConstant.TAG, "keyWord: $keyWord")
        loadHouseAdapter()
        observeData()
        onLoadMore()
        query()
    }

    private fun loadHouseAdapter(){
        adapter = HouseListAdapter(null)
        val layoutManager = LinearLayoutManager(this)
        binding.houseList.addItemDecoration(HouseItemDecoration(resources.getDimensionPixelOffset(R.dimen.dp_0), resources.getDimensionPixelOffset(R.dimen.dp_20)))
        binding.refreshLayout.setOnRefreshListener(this)
        binding.houseList.layoutManager = layoutManager
        binding.houseList.adapter = adapter
    }

    private fun observeData(){
//        viewModel.listData.observe(this, {
//            loadAdapter(it)
//        })
    }

    private fun loadAdapter(it : BaseResult<HouseResult>){
        houseResult = it.d
        if (binding.refreshLayout.isRefreshing){
            binding.refreshLayout.isRefreshing = false
        }
        val list = it.d.result
        Log.v(SDKConstant.TAG, "list: $list")
        if (list.isNullOrEmpty() && adapter?.itemCount!! <= 0){
            binding.searchNothing.visibility = View.VISIBLE
            binding.refreshLayout.visibility = View.GONE
            adapter?.clearData()
            return
        }
        binding.searchNothing.visibility = View.GONE
        binding.refreshLayout.visibility = View.VISIBLE
        adapter?.onRefreshData(list)
    }

    fun onListClick(view : View){
        when(view.id){
            R.id.houseItem -> {
                val house  = if (view.tag is House) view.tag as House else null
                Log.v(SDKConstant.TAG, "onListClick: house, $house")
                house?.let {
                    queryDetail(house)
                }
            }
        }
    }

    private fun queryDetail(house: House){
        val body = CreateWifiParam()
        val routers = ArrayList<WifiParam>(3)
        routers.add(WifiParam(0.0f, 0.0f, 1.0f))
        routers.add(WifiParam(0.3f, 0.5f, 1.0f))
        routers.add(WifiParam(0.7f, 0.8f, 0.8f))
        body.routers = routers
        viewModel.queryDetail("3FO4IM8671C8", body, this, success = {
            Log.v(SDKConstant.TAG, "detail: $it")
        })
    }

    /**
     * 数据请求
     */
    private fun query(){
        val param = HouseParam()
        param.start = start
        param.num = 20
        param.q = keyWord
        param.city_id = 175
        param.room_count = 3
        param.area_min = 0
        param.area_max = 1200
        param.isIs_standard = false
        Log.v(SDKConstant.TAG, "param: $param")
        viewModel.queryList(param, this,
            success = {
                loadAdapter(it)
            }
        )
    }

    override fun onRefresh() {
        adapter?.clearData()
        start = 0
        query()
    }

    /**
     * 上拉加载更多
     */
    private fun onLoadMore(){
        binding.houseList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (viewModel.isLoading){
                    Toast.makeText(recyclerView.context, "数据加载中……", Toast.LENGTH_SHORT).show()
                    return
                }
                if (houseResult?.isHasMore == false || houseResult?.totalCount!! <= adapter?.itemCount!!.toString()){
                    Toast.makeText(recyclerView.context, "没有更多了", Toast.LENGTH_SHORT).show()
                    return
                }
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                val lastPosition = layoutManager.findLastVisibleItemPosition()
                if (lastPosition >= (20 / 2)){
                    start++
                    query()
                }
            }
        })
    }


}